package com.neuedu.service.impl;

import com.mybatisflex.core.mask.MaskManager;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.neuedu.entity.Department;
import com.neuedu.entity.Employee;
import com.neuedu.entity.RegisterW;
import com.neuedu.entity.Visitor;
import com.neuedu.mapper.RegisterWMapper;
import com.neuedu.service.DepartmentService;
import com.neuedu.service.EmployeeService;
import com.neuedu.service.RegisterWService;
import com.neuedu.service.VisitorService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.RandomStringUtils;
import java.math.BigDecimal;
import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 挂号表 服务层实现。
 *
 * @author Administrator
 * @since 2024-06-06
 */
@Service
public class RegisterWServiceImpl extends ServiceImpl<RegisterWMapper, RegisterW> implements RegisterWService {
    @Resource
    VisitorService visitorService;
    @Resource
    EmployeeService employeeService;
    @Resource
    DepartmentService departmentService;

    @Override
    public List<RegisterW> list(Integer openId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("customer_id", openId)
                .ge("r_date", Date.valueOf(LocalDate.now()));
        return this.list(wrapper);
    }

    @Override
    public boolean add(LocalDate date, String time, Integer empId, Integer vId, BigDecimal price) {
        MaskManager.skipMask();
        Visitor visitor = visitorService.getById(vId);
        MaskManager.restoreMask();
        Employee emp = employeeService.getById(empId);
        Department dept = departmentService.getById(emp.getDeptmentId());
        RegisterW registerW = new RegisterW();
        String orderNo =  LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + RandomStringUtils.random(6, false, true);
        registerW.setOrderNo(orderNo);
        registerW.setVId(vId);
        registerW.setName(visitor.getName());
        registerW.setSex(visitor.getSex());
        registerW.setIdCard(visitor.getIdCard());
        registerW.setBirthDay(visitor.getBirthday());
        registerW.setCustomerId(visitor.getCustomerId());
        registerW.setRDate(Date.valueOf(date));
        registerW.setRTime(time);
        registerW.setDeptId(emp.getDeptmentId());
        registerW.setDeptName(dept.getDeptName());
        registerW.setEmpId(empId);
        registerW.setEmpName(emp.getRealname());
        registerW.setRegisterLevel(emp.getRegistLevelId());
        registerW.setMoney(price);
        return this.save(registerW);
    }
}
